package com.hicorp.segment.utils;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 将前端传入的表名格式 table-name 转化为table_name
 *      去除前后空格防止 table-name = "      "
 *      去除中间空格防止表名的sql注入
 *      "table-name or 1 = 1"   ==>   "table_nameor 1=1"
 */
@Component
public class TableNameUtil {

    public static String replaceTableName (String tableName) {

        try{
            if(!StringUtils.hasLength(tableName.trim())){
                throw new Exception("表名转化失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return tableName.replace("-","_").replace(" ","");
    }
}
